
//  myTableViewCell.swift
//  qiubai
//
//  Created by Qingke on 16/5/25.
//  Copyright © 2016年 Qingke. All rights reserved.
//

import UIKit

class myTableViewCell: UITableViewCell {

    
   
    @IBOutlet weak var iconImage: UIImageView!
    @IBOutlet weak var userNameLabel: UILabel!
    @IBOutlet weak var contentLabel: UILabel!
    @IBOutlet weak var contextImage: UIImageView!
    @IBOutlet weak var tipDlabel: UILabel!
    @IBOutlet weak var tipMLabel: UILabel!
    @IBOutlet weak var tipLLabel: UILabel!
    
    
    var data: NSDictionary?
    var user: NSDictionary?
    var heightOfCell: CGFloat?
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
  
    
    override func layoutSubviews() {
        super.layoutSubviews()
        loadDataOfCell()
    }

    //加载来自data的数据
    func loadDataOfCell() {
        if let item = data{
            print(item)
            user = item["user"] as? NSDictionary
            //1 用户名
//            if let _ = user!["login"] as? String {
            userNameLabel.text = user!["login"] as? String
            
            
            //2 用户头像
            if let (prefixUserId, userId,userIcon) = prepare_toImageURL(){

                let userImageURL = "http://pic.qiushibaike.com/system/avtnew/\(prefixUserId)/\(userId)/medium/\(userIcon)"
                self.iconImage.setWedImage(userImageURL, placeHorderImage: UIImage(named: "Photos")!)
            }
            
            //3 内容显示
            if let content = item["content"] as? String
            {
                self.contentLabel.text = content
            } else  {
                self.contentLabel.text = "无法加载"
            }
            
            //4 内容图片显示,这里由于有显示的图片大小不同会有冲突  *******待改进
            if let format = item["format"] as? NSString {
                switch format {
                case "image":
                    print(format)
                    prepare_toContentImageURl()
                default:
                    //没有图片的
                    print(format)
                    contextImage.hidden = true
                }
            }
            
            //5 点评数目，踩或者是顶
            if let votes = item["votes"] as? NSDictionary {
                //这里也用到了 //这里的id 是NSCFNumber类所以这里就需要的是一个转换,鉴于两个就不需要处理了
                let down = votes["down"] as! NSNumber
                let up = votes["up"] as! NSNumber
                tipDlabel.text = "顶(" + up.stringValue + ")"
                tipMLabel.text = "踩(" + down.stringValue  + ")"
            }
            if let commentCount = item.stringAttributeForKey("comments_count") as String? {
                tipLLabel.text = "评论(\(commentCount))"
            }
            
            //计算cell的高度
            // 通过内容，判断，如果是有图像就 加上图像的高度，没有就去掉图片高度
            
           
        }
        
    }
    
    //用来计算cell的高度
    class func cellHeightByData() -> CGFloat {
        
        return 59.0 + 5.0 + 112.0 + 100
    }
    
    func prepare_toImageURL() -> (prefixUserId:NSString,userId:NSString,userIcon:String)? {
        let icon  = user!["icon"]
        if icon as! NSObject != NSNull(){
            let userIcon = icon as! String
            if let idNumber = user!["id"] as? NSNumber {
                let userId = idNumber.stringValue as NSString
                let prefixUserId = userId.substringToIndex(userId.length - 4)
                return (prefixUserId, userId,userIcon)
                }
            }
        return nil
    }
    
    func prepare_toContentImageURl() {
        let imgSrc = self.data!["image"] as! NSString
        
        //这里的id 是NSCFNumber类所以这里就需要的是一个转换
        let imageIdNumber = self.data!["id"] as! NSNumber
        let imageId = imageIdNumber.stringValue as NSString
        
        
        let prefiximageId = imageId.substringToIndex(imageId.length - 4)
        let imageURL = "http://pic.qiushibaike.com/system/pictures/\(prefiximageId)/\(imageId)/small/\(imgSrc)"
        self.contextImage.hidden = false
        self.contextImage.setWedImage(imageURL, placeHorderImage: UIImage(named:"Photos")!)

    }
    
}
